/*
    Copyright 2011 Anishek Agarwal(anishek@gmail.com), Ashish Mishra(ashish@ashishmishra.in)
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/


package com.thoughtworks.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

public class BundlesValidatorTask extends Task {

    private FileGroup source;
    private FileGroup bundle;

    private boolean checkForUnused = true;
    private boolean checkForDuplicateKeys = true;
    private boolean checkForKeysWithSameValues = true;

    @Override
    public void execute() throws BuildException {
        Properties properties = new Properties(files(bundle));
        if (hasUnusedProperties(properties) | hasDuplicateKeys(properties) | hasKeysWithSameValues(properties)) {
            throw new BuildException("There are unused/duplicate properties");
        }
    }

    private boolean hasUnusedProperties(Properties properties) {
        if (!checkForUnused) return false;
        List<String> unusedProperties = properties.unused(files(source));
        if (!unusedProperties.isEmpty()) {
            System.out.println("Unused Properties are: ");
            System.out.println(unusedProperties.toString());
            return true;
        }
        return false;
    }

    private boolean hasKeysWithSameValues(Properties properties) {
        if (!checkForKeysWithSameValues) return false;
        Store<String, PropertyLocator> duplicates = properties.keysWithSameValues();
        if (duplicates.isEmpty()) return false;
        System.out.println("Different Keys having same values are:");
        System.out.println(duplicates);
        return true;
    }

    private boolean hasDuplicateKeys(Properties properties) {
        if (!checkForDuplicateKeys) return false;
        Store<String, File> duplicates = properties.duplicateKeyDeclarations();
        if (duplicates.isEmpty()) return false;
        System.out.println("Duplicate properties are:");
        System.out.println(duplicates);
        return true;
    }

    private Set<File> files(FileGroup fileGroup) {
        Set<File> files = new HashSet<File>();
        for (FileSet fileset : fileGroup.getFilesets()) {
            DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
            File baseDir = ds.getBasedir();
            String[] includedFiles = ds.getIncludedFiles();
            for (String s : includedFiles) {
                files.add(new File(baseDir.getAbsolutePath() + File.separatorChar + s));
            }
        }
        return files;
    }

    public void setCheckForUnused(boolean checkForUnused) {
        this.checkForUnused = checkForUnused;
    }

    public void setCheckForDuplicateKeys(boolean checkForDuplicateKeys) {
        this.checkForDuplicateKeys = checkForDuplicateKeys;
    }

    public void setCheckForKeysWithSameValues(boolean checkForKeysWithSameValues) {
        this.checkForKeysWithSameValues = checkForKeysWithSameValues;
    }

    public FileGroup createSource() {
        this.source = new FileGroup();
        return this.source;
    }

    public FileGroup createBundle() {
        this.bundle = new FileGroup();
        return bundle;
    }

    public static class FileGroup {
        private Vector<FileSet> filesets = new Vector<FileSet>();

        public void addFileset(FileSet fileset) {
            filesets.add(fileset);
        }

        public Vector<FileSet> getFilesets() {
            return filesets;
        }
    }
}
